package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.PickUpMatter;
import com.rzjm.exercise.module.service.PickUpMatterService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/module/pickUpMatter",produces ="application/json;charset=UTF-8")
public class PickUpMatterController extends BaseController {
    @Resource
    private PickUpMatterService pickUpMatterService;


    @Log("查询物料页面")
    @GetMapping("")
    @RequiresPermissions("module:pickUpMatter:pickUpMatter")
    String pickUpMatter(Model model) {
        return "module/pickUpMatter/list";
    }


    @Log("查询物料页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<PickUpMatter> list(DataTableLayUI<PickUpMatter> condition) {
        UserDO user = getUser();
        Map<String, Object> searchParams = condition.getSearchParams();
        searchParams.put("shopId",user.getShopId());
        return pickUpMatterService.pageSearchLayUI(condition);
    }


    @Log("新增物料页面")
    @GetMapping("/add")
    @RequiresPermissions("module:pickUpMatter:add")
    String add(Model model) {
        PickUpMatter pickUpMatter = new PickUpMatter();
        model.addAttribute("pickUpMatter", pickUpMatter);
        return "module/pickUpMatter/detail";
    }

    @Log("编辑物料页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:pickUpMatter:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        PickUpMatter pickUpMatter = pickUpMatterService.findById(id);
        model.addAttribute("pickUpMatter", pickUpMatter);
        return "module/pickUpMatter/detail";
    }

    @Log("新增物料")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:pickUpMatter:add")
    public R save(@Valid PickUpMatter pickUpMatter) {
        UserDO user = getUser();
        pickUpMatter.setState(1);
        pickUpMatter.setShopId(user.getShopId());
        pickUpMatterService.save(pickUpMatter);
        return R.ok();
    }


    /**
     * 修改
     */
    @Log("更新物料")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:pickUpMatter:edit")
    public R update(@Valid PickUpMatter pickUpMatter) {
        UserDO user = getUser();
        pickUpMatter.setState(1);
        pickUpMatter.setShopId(user.getShopId());
        pickUpMatterService.updateById(pickUpMatter);
        return R.ok();
    }


    /**
     * 删除
     */
    @Log("删除物料")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("module:pickUpMatter:remove")
    public R remove(Integer id) {
        UpdateWrapper<PickUpMatter> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("state", 0);
        if (pickUpMatterService.update(wrapper)) {
            return R.ok();
        }
        return R.error();
    }


    @ResponseBody
    @PostMapping("/getMatterList")
    @RequiresPermissions("module:pickUpMatter:add")
    public R getMatterList() {
        UserDO user = getUser();
        QueryWrapper<PickUpMatter> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", user.getShopId());
        List<PickUpMatter> list = pickUpMatterService.list(wrapper);
        return R.ok(0,list);
    }
}
